package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AddSuperviseDto;
import com.ruoyi.system.domain.vo.SysSupervisionPlanVo;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSupervisionPlanMapper;
import com.ruoyi.system.domain.SysSupervisionPlan;
import com.ruoyi.system.service.ISysSupervisionPlanService;

/**
 * 督查计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-31
 */
@Service
public class SysSupervisionPlanServiceImpl implements ISysSupervisionPlanService 
{
    @Autowired
    private SysSupervisionPlanMapper sysSupervisionPlanMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询督查计划
     * 
     * @param planId 督查计划主键
     * @return 督查计划
     */
    @Override
    public SysSupervisionPlan selectSysSupervisionPlanByPlanId(Long planId)
    {
        return sysSupervisionPlanMapper.selectSysSupervisionPlanByPlanId(planId);
    }

    /**
     * 查询督查计划列表
     * 
     * @param sysSupervisionPlan 督查计划
     * @return 督查计划
     */
    @Override
    public List<SysSupervisionPlanVo> selectSysSupervisionPlanList(SysSupervisionPlan sysSupervisionPlan)
    {

        List<SysSupervisionPlanVo> result = new ArrayList<>();

        List<SysSupervisionPlan> list = sysSupervisionPlanMapper.selectSysSupervisionPlanList(sysSupervisionPlan);


        List<SysUser> sysUsers = sysUserMapper.selectUserList(null);
        Map<Long, String> userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName));


        for (SysSupervisionPlan supervisionPlan : list) {

            SysSupervisionPlanVo vo = new SysSupervisionPlanVo();
            BeanUtils.copyProperties(supervisionPlan,vo);

            if (supervisionPlan.getSupervisionLeaderName()!=null){
                vo.setLeaderName(userMap.get(supervisionPlan.getSupervisionLeaderName()));
            }

            if (supervisionPlan.getSafetyInspectorName()!=null){
                vo.setSafetyName(userMap.get(supervisionPlan.getSafetyInspectorName()));
            }

            result.add(vo);
        }

        return result;
    }

    /**
     * 新增督查计划
     * 
     * @param sysSupervisionPlan 督查计划
     * @return 结果
     */
    @Override
    public int insertSysSupervisionPlan(SysSupervisionPlan sysSupervisionPlan)
    {
        sysSupervisionPlan.setCreateTime(DateUtils.getNowDate());
        return sysSupervisionPlanMapper.insertSysSupervisionPlan(sysSupervisionPlan);
    }

    /**
     * 修改督查计划
     * 
     * @param sysSupervisionPlan 督查计划
     * @return 结果
     */
    @Override
    public int updateSysSupervisionPlan(SysSupervisionPlan sysSupervisionPlan)
    {
        sysSupervisionPlan.setUpdateTime(DateUtils.getNowDate());
        return sysSupervisionPlanMapper.updateSysSupervisionPlan(sysSupervisionPlan);
    }

    /**
     * 批量删除督查计划
     * 
     * @param planIds 需要删除的督查计划主键
     * @return 结果
     */
    @Override
    public int deleteSysSupervisionPlanByPlanIds(Long[] planIds)
    {
        return sysSupervisionPlanMapper.deleteSysSupervisionPlanByPlanIds(planIds);
    }

    /**
     * 删除督查计划信息
     * 
     * @param planId 督查计划主键
     * @return 结果
     */
    @Override
    public int deleteSysSupervisionPlanByPlanId(Long planId)
    {
        return sysSupervisionPlanMapper.deleteSysSupervisionPlanByPlanId(planId);
    }

    @Override
    public AjaxResult batchAdd(AddSuperviseDto dto) {


        dto.getDataList().forEach(obj->{

            SysSupervisionPlan sysSupervisionPlan = new SysSupervisionPlan();
            sysSupervisionPlan.setStartDate(dto.getStartDate());
            sysSupervisionPlan.setEndDate(dto.getEndDate());
            sysSupervisionPlan.setProjectId(obj.getProjectId());
            sysSupervisionPlan.setSupervisionLeaderName(obj.getSupervisionLeaderName());
            sysSupervisionPlan.setSupervisionLeaderPhone(obj.getSupervisionLeaderPhone());
            sysSupervisionPlan.setSafetyInspectorName(obj.getSafetyInspectorName());
            sysSupervisionPlan.setSafetyInspectorPhone(obj.getSafetyInspectorPhone());
            sysSupervisionPlan.setVoltageLevel(obj.getVoltageLevel());

            sysSupervisionPlanMapper.insertSysSupervisionPlan(sysSupervisionPlan);

        });


        return AjaxResult.success();
    }
}
